const timeExp = /\[(\d{2,}):(\d{2})(?:\.(\d{2,3}))?]/g

const STATE_PAUSE = 0
const STATE_PLAYING = 1

const tagRegMap = {
    title: 'ti',
    artist: 'ar',
    album: 'al',
    offset: 'offset',
    by: 'by'
}

function noop() {
}

export default class Lyric {
    constructor(lrc, hanlder = noop) {
        this.lrc = lrc
        this.tags = {}
        this.lines = []
        this.handler = hanlder
        this.state = STATE_PAUSE
        this.curLine = 0

        this._init()
    }

    _init() {
        this._initTag()

        this._initLines()
        console.log(this.lines)
    }

    _initTag() {
        for (let tag in tagRegMap) {
            const matches = this.lrc.match(new RegExp(`\\[${tagRegMap[tag]}:([^\\]]*)]`, 'i'))
            console.log(matches)
            this.tags[tag] = matches && matches[1] || ''
        }
    }

    _initLines() {
        if (this.lrc.includes('\r\n')) {
            var lines = this.lrc.split('\r\n')
        } else if (this.lrc.includes('\n\r')) {
            var lines = this.lrc.split('\n\r')
        } else if (this.lrc.includes('\n')) {
            var lines = this.lrc.split('\n')
        } else if (this.lrc.includes('\r')) {
            var lines = this.lrc.split('\r')
        }
        const offset = parseInt(this.tags['offset']) || 0
        for (let i = 0; i < lines.length; i++) {
            const line = lines[i]
            let result = timeExp.exec(line)
            // console.log(result)
            if (result) {
                const txt = line.replace(timeExp, '').trim()
                if (txt) {
                    this.lines.push({
                        time: result[1] * 60 * 1000 + result[2] * 1000 + (result[3].length === 2 ? result[3] * 10 : (result[3].length === 3 ? result[3] * 1 : 0)) + offset,
                        txt
                    })

                }
            }
        }
        // let bool = false;
        // this.lines = this.lines.filter(item => {
        //     if (item.txt + '' === '' || item.txt + '' === 'null' || item.txt + '' === 'undefined' || item.txt + '' === 'NaN') {
        //         bool = true
        //         return false;
        //     } else {
        //         return true;
        //     }
        // })
        // console.log(bool)
        // if (bool) {
        //     alert('歌词有问题')
        // }
        this.lines.sort((a, b) => {
            return a.time - b.time
        })

    }

    _findCurNum(time) {
        for (let i = 0; i < this.lines.length; i++) {
            console.log(this.lines[i])
            if (time <= this.lines[i].time) {
                return i
            }
        }
        return this.lines.length - 1


    }

    _callHandler(i) {
        if (i < 0) {
            return
        }

        this.handler({
            txt: this.lines[i].txt,
            lineNum: i
        })
    }

    _playRest() {
        let line = this.lines[this.curNum]
        let delay = line.time - (+new Date() - this.startStamp)

        this.timer = setTimeout(() => {
            console.log(1044444)
            this._callHandler(this.curNum++)
            if (this.curNum < this.lines.length && this.state === STATE_PLAYING) {
                this._playRest()
            }
        }, delay)
    }

    play(startTime = 0, skipLast) {
        console.log(startTime)
        if (!this.lines.length) {
            return
        }
        this.state = STATE_PLAYING
        console.log(this.state)
        this.curNum = this._findCurNum(startTime)
        this.startStamp = +new Date() - startTime
        console.log(1233333, skipLast)
        if (!skipLast) {
            this._callHandler(this.curNum - 1)
        }
        console.log(129999, skipLast)
        if (this.curNum < this.lines.length) {
            clearTimeout(this.timer)
            this._playRest()
        }
    }

    togglePlay() {
        console.log('togglePlay')
        var now = +new Date()
        console.log(this.state)
        if (this.state === STATE_PLAYING) { // 播放状态
            console.log(1111)
            this.stop()
            this.pauseStamp = now
        } else {
            console.log(2222)
            this.state = STATE_PLAYING
            // clearTimeout(this.timer) // 自己加的代码
            this.play((this.pauseStamp || now) - (this.startStamp || now), true)
            this.pauseStamp = 0
        }
    }

    stop() {
        this.state = STATE_PAUSE
        console.log(this.state)
        clearTimeout(this.timer)
    }

    seek(offset) {
        // clearTimeout(this.timer) // 自己加的代码
        this.play(offset)
    }


}

